package com.aismy.admin.service.impl;

import com.aismy.admin.entity.ConsultationMessage;
import com.aismy.admin.entity.ConsultationRecord;
import com.aismy.admin.mapper.ConsultationMessageMapper;
import com.aismy.admin.mapper.ConsultationRecordMapper;
import com.aismy.admin.service.ConsultationMessageService;
import com.aismy.admin.service.ConsultationService;
import com.aismy.admin.websocket.ConsultationWebSocketService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ConsultationServiceImpl extends ServiceImpl<ConsultationRecordMapper, ConsultationRecord> implements ConsultationService {

    private final ConsultationMessageService messageService;
    private final ConsultationWebSocketService webSocketService;

    @Override
    @Transactional
    public ConsultationRecord createConsultation(Long consultantId, Long userId) {
        ConsultationRecord record = new ConsultationRecord();
        record.setConsultantId(consultantId);
        record.setUserId(userId);
        record.setStatus(1); // 进行中
        this.save(record);
        return record;
    }

    @Override
    @Transactional
    public void endConsultation(Long consultationId) {
        ConsultationRecord record = this.getById(consultationId);
        if (record != null) {
            record.setStatus(2); // 已结束
            record.setEndedAt(LocalDateTime.now());
            this.updateById(record);
        }
    }

    @Override
    @Transactional
    public void sendMessage(ConsultationMessage message) {
        // 保存消息
        messageService.save(message);
        // 推送消息
        webSocketService.sendMessage(message);
    }

    @Override
    public Page<ConsultationRecord> getUserConsultations(Long userId, Integer page, Integer size) {
        return this.page(
            new Page<>(page, size),
            new LambdaQueryWrapper<ConsultationRecord>()
                .eq(ConsultationRecord::getUserId, userId)
                .orderByDesc(ConsultationRecord::getCreatedAt)
        );
    }

    @Override
    public Page<ConsultationRecord> getConsultantConsultations(Long consultantId, Integer page, Integer size) {
        return this.page(
            new Page<>(page, size),
            new LambdaQueryWrapper<ConsultationRecord>()
                .eq(ConsultationRecord::getConsultantId, consultantId)
                .orderByDesc(ConsultationRecord::getCreatedAt)
        );
    }

    @Override
    public void markAsRead(List<Long> messageIds) {
        messageService.markAsRead(messageIds);
    }

    @Override
    public Page<ConsultationMessage> getHistoryMessages(Long consultationId, Integer page, Integer size) {
        return messageService.getHistoryMessages(consultationId, page, size);
    }
} 